package com.yanqu.road.entity.activity.tongbing.data;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossTongBingBattlefield {

    private long createTime; // 战场创建时间
    private long helpNum; // 求援数量

    private int drumTimes; // 战鼓剩余场次
    private long useDrumTime; // 使用战鼓时间

    private long lastBattleTime; // 上次战斗时间

    private int battledNum; // 战斗场次

    // 分战场
    private int maxPartId;
    private Map<Integer, CrossTongBingBattlefieldPart> partMap = new ConcurrentHashMap<>();

    // 战场排行
    private LinkedList<CrossTongBingBattlefieldRank> battlefieldRankList = new LinkedList<>();

    // 连胜
    private Map<String, Integer> winTimes = new ConcurrentHashMap<>();

    public long getUseDrumTime() {
        return useDrumTime;
    }

    public void setUseDrumTime(long useDrumTime) {
        this.useDrumTime = useDrumTime;
    }

    public int getMaxPartId() {
        return maxPartId;
    }

    public void setMaxPartId(int maxPartId) {
        this.maxPartId = maxPartId;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public int getDrumTimes() {
        return drumTimes;
    }

    public void setDrumTimes(int drumTimes) {
        this.drumTimes = drumTimes;
    }

    public long getHelpNum() {
        return helpNum;
    }

    public void setHelpNum(long helpNum) {
        this.helpNum = helpNum;
    }

    public long getLastBattleTime() {
        return lastBattleTime;
    }

    public void setLastBattleTime(long lastBattleTime) {
        this.lastBattleTime = lastBattleTime;
    }

    public Map<Integer, CrossTongBingBattlefieldPart> getPartMap() {
        return partMap;
    }

    public void setPartMap(ConcurrentHashMap<Integer, CrossTongBingBattlefieldPart> partMap) {
        this.partMap = partMap;
    }

    public LinkedList<CrossTongBingBattlefieldRank> getBattlefieldRankList() {
        // 用这个getBattlefieldRankNewList
        return battlefieldRankList;
    }

    public void setBattlefieldRankList(LinkedList<CrossTongBingBattlefieldRank> battlefieldRankList) {
        this.battlefieldRankList = battlefieldRankList;
    }

    public Map<String, Integer> getWinTimes() {
        return winTimes;
    }

    public void setWinTimes(ConcurrentHashMap<String, Integer> winTimes) {
        this.winTimes = winTimes;
    }

    public int getBattledNum() {
        return battledNum;
    }

    public void setBattledNum(int battledNum) {
        this.battledNum = battledNum;
    }

    // =============================

    public void rankChange(long userId, long addZhanGong, long addKillNum) {
        synchronized (this) {
            boolean find = false;
            for (CrossTongBingBattlefieldRank rank : battlefieldRankList) {
                if (rank.getUserId() == userId) {
                    rank.setZhanGong(rank.getZhanGong() + addZhanGong);
                    rank.setKillNum(rank.getKillNum() + addKillNum);
                    find = true;
                    break;
                }
            }
            if (!find) {
                battlefieldRankList.add(new CrossTongBingBattlefieldRank(userId, addZhanGong, addKillNum));
            }
            battlefieldRankList.sort(new Comparator<CrossTongBingBattlefieldRank>() {
                @Override
                public int compare(CrossTongBingBattlefieldRank o1, CrossTongBingBattlefieldRank o2) {
                    if (o2.getZhanGong() != o1.getZhanGong()) {
                        return Long.compare(o2.getZhanGong(), o1.getZhanGong());
                    }
                    return Long.compare(o2.getKillNum(), o1.getKillNum());
                }
            });
        }
    }

    public LinkedList<CrossTongBingBattlefieldRank> getBattlefieldRankNewList() {
        return new LinkedList<>(battlefieldRankList);
    }

}
